package com.ztgf.commodity.api.cms;

import com.alibaba.fastjson.JSONObject;
import com.ztgf.commodity.models.dto.CommodityAuditLogDTO;
import com.ztgf.commodity.models.service.ICommodityAuditLogService;
import com.ztgf.commodity.models.vo.CommodityAuditLogVO;
import com.ztgf.commodity.models.vo.CommodityRegardVO;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-05-26 17:00
 * Description:
 */
@RestController
@RequestMapping("/cms/commodityAuditLog")
@Slf4j
@Api(value = "CommodityAuditLogController", tags = "商品审核日志表接口")
public class CommodityAuditLogController {

    @DubboReference
    private ICommodityAuditLogService commodityAuditLogService;

    @PostMapping("/getById")
    @ApiOperation(value = "根据id查找商品审核日志表", notes = "根据id查找商品审核日志表")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CommodityAuditLogVO> getById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(commodityAuditLogService.getByLogId(id));
    }

    @PostMapping("/queryPageList")
    @ApiOperation(value = "后台管理系统商品审核日志列表", notes = "后台管理系统商品审核日志列表")
    public Result queryPageList(CommodityAuditLogVO vo) {
        return new Result<>(commodityAuditLogService.queryCommodityAuditLogList(vo));
    }

    @ApiOperation(value = "根据商品id获取审核记录列表",notes = "根据商品id获取审核记录列表")
    @PostMapping("/getLogsByCommodityId")
    public Result<List<CommodityAuditLogVO>> getLogsByCommodityId(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<CommodityAuditLogVO> commodityAuditLogVOS = commodityAuditLogService.getLogsByCommodityId(id);
        return new Result(commodityAuditLogVOS);
    }

    @ApiOperation(value = "根据商品id获取审核记录列表",notes = "根据商品id获取审核记录列表")
    @PostMapping("/getLogDetailById")
    public Result<CommodityAuditLogVO> getLogDetailById(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        CommodityAuditLogDTO dto = commodityAuditLogService.selectById (id);
        CommodityAuditLogVO vo = new CommodityAuditLogVO ();
        BeanUtils.copyProperties(dto, vo);
        if(vo.getParams () != null){
            CommodityRegardVO commodityRegardVO = JSONObject.parseObject(vo.getParams (), CommodityRegardVO.class);
            vo.setCommodityRegardVO (commodityRegardVO);
        }
        return new Result(vo);
    }
}
